﻿/**********************************************
 * 功能名称：资源数量录入界面
编写人：  杨莹
建立时间： 杨 2012年7月10日
 *修改时间： 杨莹 2012年7月11日
说明：
 * 列表显示数量的归集起始点。录入资源数量，并保存。

 **************************************************************/

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hisun.Models;
using Hisun.Controllers;

namespace Hisun.View.Resource
{
    public partial class Resource_SL : System.Web.UI.Page
    {
        HisunDBDataContext db = new HisunDBDataContext();
        public string alert;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindMyData();


            }
        }

        private void BindMyData()
        {

            var MyGridViewQuery = from a in db.TABC_JS_ResourceSetup
                                  join b in db.TABC_JS_CostCenter on a.作业中心ID equals b.作业中心ID
                                  join c in
                                      (from e in db.TABC_JC_CostCenterItems where e.会计期间ID == Application["accountPeriod"].ToString() && e.成本细项ID == "X" select e)
                                  on a.作业中心ID equals c.作业中心ID into mytemps
                                  where a.数量 == true
                                  orderby b.作业中心名称
                                  from temp in mytemps.DefaultIfEmpty()
                                  select new
                                  {

                                      a.作业中心ID,
                                      b.作业中心名称,
                                      数量 = (temp == null ? 0 : temp.数量)

                                  };

            GridView_SL.DataSource = MyGridViewQuery;
            GridView_SL.DataBind();

        }

        //填充金额后，提交所有的数据。
        protected void Button1_Click(object sender, EventArgs e)
        {


            var MyDeleteRow = from a in db.TABC_JC_CostCenterItems
                              where
                                  a.成本细项ID == "X" && a.会计期间ID == Application["accountPeriod"].ToString()
                                  && !(from b in db.TABC_JS_ResourceSetup
                                       where b.数量 == true
                                       select b.作业中心ID.ToString()).Contains(a.作业中心ID.ToString())
                              select a;

            db.TABC_JC_CostCenterItems.DeleteAllOnSubmit(MyDeleteRow);
            db.SubmitChanges();

            try
            {
                for (int i = 0; i < GridView_SL.Rows.Count; i++)
                {
                    GridViewRow myrows = GridView_SL.Rows[i];


                    var IsExistRow = from a in db.TABC_JC_CostCenterItems
                                     where a.作业中心ID == Convert.ToInt32(myrows.Cells[0].Text)
                                     && a.成本细项ID == "X"
                                     && a.会计期间ID == Application["accountPeriod"].ToString()
                                     select a;

                    if (IsExistRow.Count() == 0)
                    {

                        TABC_JC_CostCenterItems MyNewRow = new TABC_JC_CostCenterItems();

                        MyNewRow.公司ID = "hisun";
                        MyNewRow.作业中心ID = Convert.ToInt32(myrows.Cells[0].Text);
                        MyNewRow.会计期间ID = Application["accountPeriod"].ToString();
                        MyNewRow.成本细项ID = "X";
                        MyNewRow.数量 = Convert.ToDecimal(((TextBox)(myrows.Cells[2].FindControl("TextBox_Money"))).Text);
                        db.TABC_JC_CostCenterItems.InsertOnSubmit(MyNewRow);
                    }
                    else if (IsExistRow.Count() == 1)
                    {
                        var myEditRow = IsExistRow.Single();
                        myEditRow.数量 = Convert.ToDecimal(((TextBox)(myrows.Cells[2].FindControl("TextBox_Money"))).Text);


                    }

                }
                db.SubmitChanges();
                alert = "<script>alert('更新数据成功'); </script>";
                BindMyData();
            }
            catch
            {
              alert = "<script>alert('更新数据失败'); </script>";


            }
        }


        //定义全局变量，并对列数据绑定事件进行编程。以显示汇总数据
        decimal totalcash = 0;
        protected void GridView_SL_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                totalcash += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "数量"));
            }
            else if (e.Row.RowType == DataControlRowType.Footer)
            {
                e.Row.Cells[0].Text = "合计";
                e.Row.Cells[2].Text = String.Format("{0:N2}", totalcash);
            }
        }
    }
}
